package practice.tmp;

public class Solution02 {
    public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
        ListNode dummy = new ListNode(-1);
        ListNode cur = dummy;
        int num = 0;
        while (l1 != null || l2 != null) {
            int x = l1 == null ? 0 : l1.val;
            int y = l2 == null ? 0 : l2.val;
            num += x + y;
            cur.next = new ListNode(num % 10);
            cur = cur.next;
            num /= 10;
            if (l1 != null) l1 = l1.next;
            if (l2 != null) l2 = l2.next;
        }
        if (num != 0) cur.next = new ListNode(num);
        return dummy.next;
    }
}
